package com.whnk.report.service.impl;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.utils.StringUtils;
import com.whnk.report.constant.RptConstants;
import com.whnk.report.domain.CompPd;
import com.whnk.report.domain.EChartsData;
import com.whnk.report.domain.SeriesData;
import com.whnk.report.enums.RptMagicType;
import com.whnk.report.mapper.CompPdMapper;
import com.whnk.report.service.ICompPdService;
import com.whnk.report.utils.RptUtils;

/**
 * 报表统计 服务层实现
 * 
 * @author whwhnk
 * @date 2019-02-27
 */
@Service
public class CompPdServiceImpl implements ICompPdService 
{
	@Autowired
	private CompPdMapper compPdMapper;
	
	/**
     * 查询供应商按年月统计人天数报表
     * 
     * @param compPd
     * @return 统计集合
     */
	@Override
	public EChartsData selectCompPdRpt(CompPd compPd)
	{
		
		EChartsData eChartData = new EChartsData();
        List<CompPd> list = compPdMapper.selectCompPdRpt(compPd);
        
        List<String> xData = new ArrayList<String>();
        List<SeriesData> series = new ArrayList<SeriesData>();
        List<String> legendData = new ArrayList<String>();
        
        SeriesData seriesData = new SeriesData();
        List<String> data = new ArrayList<String>();
        
        for(int i=0; i<list.size();i++) {
        	CompPd item = list.get(i);
        	String comp = item.getEmpComp();// 供应商名称

        	// 不统计供应商名称为空的情况
        	if(StringUtils.isEmpty(comp))
        		continue;
        	
        	// 横坐标：年月
        	String signYm = item.getSignYm();
        	if(!RptUtils.checkRepeat(xData,signYm)) 
        		xData.add(signYm); 
        	
        	if(!RptUtils.checkRepeat(legendData,comp)) {
        		
        		if(legendData.size()>0) 
        			fillSeries(series,seriesData,data,legendData.get(legendData.size()-1),RptMagicType.BAR);
        		
        		seriesData = new SeriesData();
        		data = new ArrayList<String>();
        		
        		legendData.add(comp);
        	}
        	data.add(String.valueOf(item.getSignPd())); // 填充数据
        	
        	// 最后一个公司的数据填充到series中
        	if(i==list.size()-1) 
        		fillSeries(series,seriesData,data,legendData.get(legendData.size()-1),RptMagicType.BAR);
        }
        
        eChartData.setTitle(RptConstants.COMP_PD_TITLE);
        eChartData.setLegendData(legendData);
        eChartData.setxData(xData);
        eChartData.setSeriesData(series);
        
	    return eChartData;
	}
	
	/**
	 * 填充报表数据到series中
	 * 
	 * @param series
	 * @param seriesData
	 * @param data
	 * @param name
	 * @param type
	 */
	private void fillSeries(List<SeriesData> series,SeriesData seriesData, List<String> data, String name,RptMagicType type) {
		seriesData.setName(name);
		seriesData.setType(type.getInfo());
		seriesData.setData(data);
		series.add(seriesData);
	}
	
}
